package com.vr.xuecheng.learning.listener;

import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.learning.constant.RabbitMqConstants;
import com.vr.xuecheng.learning.service.IChooseCourseService;
import com.vr.xuecheng.messagesdk.model.entity.MqMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author hzh
 * @date 2025/04/10
 * 监听支付通知的队列
 */
@Log4j2
@Component
@RequiredArgsConstructor
public class PayNotifyListener {
    private final IChooseCourseService chooseCourseService;
    @RabbitListener(queues = RabbitMqConstants.PAY_NOTIFY_QUEUE)
    public void handlePayNotify(MqMessage mqMessage) {
        System.out.println(mqMessage.getMessageType());
        System.out.println("收到支付结果通知");
        String messageType = mqMessage.getMessageType();
        String businessKey2 = mqMessage.getBusinessKey2();
        if ("60201".equals(businessKey2)&&RabbitMqConstants.MESSAGE_TYPE.equals(messageType)) {
            // 表示是购买课程的支付通知
            // 选课id
            String businessKey1 = mqMessage.getBusinessKey1();
            boolean b = chooseCourseService.updateChooseCourseSuccess(Long.parseLong(businessKey1));
            if (!b) {
                // 处于某种原因，选课失败
                XueChengPlusException.cast("收到支付结果，添加选课失败");
            }


        }
    }
}
